<template>
	<div>
	<view>进行评测</view>
	<view class="content">
	        <!-- 问题标题 -->
			<view v-if="diseasePaper!=null">
			    <h3 v-text="diseasePaper.paperDialogue"></h3>
			</view>
	        <view v-if="question!=null">
	            <h3 v-text="question.askTopic"></h3>
	        </view>
	        <!-- 问题答案 -->
	        <view v-if="answes!=null">
	            <!-- <view v-text="answers[0].answerText"></view> -->
	                <radio-group @change="onAnswerChange">
	                    <label class="radio" v-for="item in answes">
	                        <radio :value="''+item.answerId" />{{item.answerName}}
	                    </label>
	                </radio-group>
	        </view v-if="diseasePaper!=null">
	        <!-- 提交问题 -->
	        <view v-if="question!=null">
	            <button type="primary" @click="replyReviewQuestion">提交答案</button>
	        </view>
	        <!-- 全部回答完在进行操作 -->
	       <!-- <view>
	            <text>请耐心等待30秒，健康报告正在生成</text>
	        </view> -->
		</view>
	</div>	
</template>

<script>
	export default {
	  data(){
	    return {
			userId:'',
			evaluatingId:0,
			question:null,
			answes:null,
			answerId:0,
			askId:0,
			questionnaireId:'',
			diseasePaper:null,
		}
		   },
	   onLoad(fun){
		    this.userId= uni.getStorageSync('userId');
		   this.questionnaireId=fun.questionaireId
		   this.beginreview()
	    },
	   methods:{
		   //首先进行评测
			beginreview(){
				console.log(this.userId)	
				console.log(this.questionnaireId)	
				uni.request({
				    url: `http://localhost:7000/review/beginreview`, //接口地址。
				    data: {
						userId :this.userId,
						questionnaireId: this.questionnaireId
					},
				    header: {
				        'custom-header': 'hello' //自定义请求头信息
				    },
					method:'POST',
				    success: (res) => {
						console.log(res.data.data)
						this.evaluatingId=res.data.data.evaluatingId
						this.getReviewQuestion()
				    }
				});	
			},
			//获取问题
			getReviewQuestion(){
				console.log(this.reviewId)
				this.question=null
				this.answes=null
				uni.request({
				    url: `http://localhost:7000/review/getEvaluationAsk/${this.evaluatingId}`, //接口地址。
				    header: {
				        'custom-header': 'hello' //自定义请求头信息
				    },
					method:'POST',
				    success: (res) => {
						if(res.data.code ==200){
						this.question=res.data.data.ask
						this.answes=res.data.data.answer
						this.askId=res.data.data.ask.askId
						console.log(this.question)
						console.log(this.answes)	
						}else if(res.data.code==600){
							this.gettbuserEvaluatingInsert()
						}
				    }
				});	
			},
			//提交答案
			replyReviewQuestion(){
				uni.request({
				    url: `http://localhost:7000/review/getUserEvaluatingAsk`, //接口地址。
					data: {
						userId: this.userId,
						evaluatingId:this.evaluatingId,
						askId:this.askId,
						answerId:this.answerId,
						questionaireId:this.questionnaireId
					},
				    header: {
				        'custom-header': 'hello' //自定义请求头信息
				    },
					method:'POST',
				    success: (res) => {
						//console.log(res.data)
						this.getReviewQuestion()
						this.smartDialogueAnswer()
				    }
				});	
			},
			//生成智能对话
			smartDialogueAnswer(){
				uni.request({
				    url: `http://localhost:7000/review/smartDialogueAnswer/${this.answerId}`, //接口地址。
				    header: {
				        'custom-header': 'hello' //自定义请求头信息
				    },
					method:'POST',
				    success: (res) => {
						this.diseasePaper=res.data.data
				    }
				});	
			},
			//生成评测报告
			gettbuserEvaluatingInsert(){
				uni.request({
				    url: `http://localhost:7000/review/gettbuserEvaluatingInsert/${this.evaluatingId}`, //接口地址。
				    header: {
				        'custom-header': 'hello' //自定义请求头信息
				    },
					method:'POST',
				    success: (res) => {
						console.log(res.data.data)
						uni.showToast({
							title: "评测报告正在生成请耐心等待10秒钟",
							icon: 'loading',    //如果要纯文本，不要icon，将值设为'none'
							duration: 6000    //持续时间为 2秒
						}),
						setTimeout(function(){
							uni.navigateTo({
								url:"../details/index?evaluatingid="+res.data.data
							}) 
						},10000)
				    }
				});	
			},
			 onAnswerChange(event) {
			    console.log(event.detail.value);
			    this.answerId = event.detail.value
			}
	    }
	}
</script>

<style>
</style>
